package com.lyk.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.common.dao.BaseDao;
import com.common.entity.A_markup;
import com.common.entity.PageBean;
import com.lyk.dao.A_markupDao;

public class A_markupDaoImpl extends BaseDao<A_markup> implements A_markupDao {

	@Override
	public A_markup findById(Integer n) {
		List<Object> paramList=new ArrayList<Object>();
		StringBuffer sf=new StringBuffer();
		sf.append(" select * from a_markup where markup_id = ? ");
		paramList.add(n);
		List<A_markup> list = findBySql(sf.toString(), paramList.toArray());
		return (list!=null&&list.size()>0)?list.get(0):null;
	}

	@Override
	public int changemarkup(A_markup markup) {
		String sql = "update a_markup set markup_name=?,markup_money=? where markup_id = ?";
		List<Object> paramList=new ArrayList<Object>();
		paramList.add(markup.getMarkup_name());
		paramList.add(markup.getMarkup_money());
		paramList.add(markup.getMarkup_id());
		return this.executeUpdate(sql, paramList.toArray());
	}

	@Override
	public List<A_markup> findByCondition(Map<String, Object> mp) {
		List<Object> paramList=new ArrayList<Object>();
		StringBuffer sf=new StringBuffer();
		sf.append(" select * from a_markup where 1=1 ");
		if(mp!=null && mp.size()>0){
				if(mp.get("markup_id")!=null){
					sf.append("and markup_id = ? ");
					paramList.add(mp.get("markup_id"));
				}
		}
		return findBySql(sf.toString(), paramList.toArray());
	}
	
	@Override
	public int delA_markup(Integer n) {
		String sql = "delete a_markup where markup_id = ?";
		return this.executeUpdate(sql, new Object[]{n});
	}
	
	public A_markup getEntity(ResultSet rs){
		try {
			A_markup markup = new A_markup();
			markup.setMarkup_id(rs.getInt("markup_id"));
			markup.setMarkup_name(rs.getString("markup_name"));
			markup.setMarkup_money(rs.getInt("markup_money"));
			return markup;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public int saveEmp(A_markup markup) {
		String sql="insert into a_markup(markup_id,markup_name,markup_money) values(seq_a_markup.nextval,?,?)";
		List<Object> paramList=new ArrayList<Object>();
		paramList.add(markup.getMarkup_name());
		paramList.add(markup.getMarkup_money());
		return this.executeUpdate(sql, paramList.toArray());
	}

	@Override
	public PageBean<A_markup> findPageByCondition(int pageIndex, int pageSize,
			Map<String, Object> mp) {
		PageBean<A_markup> pageBean=new PageBean<A_markup>();
		pageBean.setPageIndex(pageIndex);
		pageBean.setPageSize(pageSize);
		List<Object> paramList=new ArrayList<Object>();
		StringBuffer querySql=new StringBuffer();
		querySql.append(" select * from a_markup where 1=1");
		if(mp!=null && mp.size()>0){
			if(mp.get("markup_id")!=null){
				querySql.append("and markup_id = ? ");
				paramList.add(mp.get("markup_id"));
			}
		}
		pageBean.setTotalCount(this.getCount(querySql.toString(), paramList.toArray()));
		if(pageBean.getTotalCount()>0){
			List<A_markup> list = findPage(pageBean, paramList, querySql);
			pageBean.setList(list);
		}
		
		return pageBean;
	}
	
	private List<A_markup> findPage(PageBean<A_markup> pageBean,
			List<Object> paramList, StringBuffer querySql) {
		List<A_markup> list=new ArrayList<A_markup>();
		StringBuffer queryPageSql=new StringBuffer();
		queryPageSql.append(" select * from (");
		queryPageSql.append("  select tmp.*,rownum rn from ( ");
		queryPageSql.append(querySql.toString());
		queryPageSql.append("  ) tmp ");
		queryPageSql.append(" ) where rn>? and rn<=? ");
		int startRow=(pageBean.getPageIndex()-1)*pageBean.getPageSize();
		int endRow=pageBean.getPageIndex()*pageBean.getPageSize();
		paramList.add(startRow);
		paramList.add(endRow);
		Connection conn=this.getConnection();
		PreparedStatement prs=null;
		ResultSet rs=null;
		try {
			conn=this.getConnection();
			prs=conn.prepareStatement(queryPageSql.toString());
			Object[] objs=paramList.toArray();
			if(objs!=null && objs.length>0){
				for(int i=0;i<objs.length;i++){
					prs.setObject(i+1, objs[i]);
				}
			}
			rs=prs.executeQuery();
			
			while(rs.next()){
				A_markup markup = new A_markup();
				markup.setMarkup_id(rs.getInt("markup_id"));
				markup.setMarkup_name(rs.getString("markup_name"));
				markup.setMarkup_money(rs.getInt("markup_money"));
				list.add(markup);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
}
